Ext.define('PJ.view.unitregistrationmanagement.CloseUserList', {
    extend: 'AM.default.DefaultList',
    alias: ['widget.CloseUserList'],
    requires: ['AM.default.Month'//
               ],
   isHavePage:false,
    initComponent: function() {
    	var i=0;
    	var reportPhone=new Array();
    	function contains(arr, obj) {  
    	    var i = arr.length;  
    	    while (i--) {  
    	        if (arr[i] === obj) {  
    	            return true;  
    	        }  
    	    }  
    	    return false;  
    	}
    	function removeOne(arr,val){
    		var index = arr.indexOf(val);
    	    if (index > -1) {
    	    	arr.splice(index, 1);
    	    }
    	    return arr;
    	}
        var sexStore = Ext.create("Ext.data.ArrayStore", {
            singleton: true,
            fields: [{
                name: 'value',
                mapping: "dataCode",
                type: 'string'
            },
            {
                name: 'text',
                mapping: "dataName",
                type: 'string'
            }],
            proxy: {
                type: 'ajax',
                url: this.grid.win.store.sexUrl + '?typeCode=1220',
                reader: Ext.create('Ext.data.reader.Json', {
                    type: 'json',
                    root: 'data',
                    successProperty: 'success'
                })
            },
            autoLoad: true
        });
        var folkStore = Ext.create("Ext.data.ArrayStore", {
            singleton: true,
            fields: [{
                name: 'value',
                mapping: "dataCode",
                type: 'string'
            },
            {
                name: 'text',
                mapping: "dataName",
                type: 'string'
            }],
            proxy: {
                type: 'ajax',
                url: this.grid.win.store.sexUrl + '?typeCode=1224',
                reader: Ext.create('Ext.data.reader.Json', {
                    type: 'json',
                    root: 'data',
                    successProperty: 'success'
                })
            },
            autoLoad: true
        });
        var rankStore = Ext.create("Ext.data.ArrayStore", {
            singleton: true,
            fields: [{
                name: 'value',
                mapping: "dataCode",
                type: 'string'
            },
            {
                name: 'text',
                mapping: "dataName",
                type: 'string'
            }],
            proxy: {
                type: 'ajax',
                url: this.grid.win.store.rankCodeUrl + '?typeCode=1228',
                reader: Ext.create('Ext.data.reader.Json', {
                    type: 'json',
                    root: 'data',
                    successProperty: 'success'
                })
            },
            autoLoad: true
        });
        function getStockType1(value) {
            var rowIndex = sexStore.find("value", "" + value);
            if (rowIndex < 0) return value;
            var record = sexStore.getAt(rowIndex);
            return record ? record.get('text') : '';
        };
        function getStockType2(value) {
            var rowIndex = folkStore.find("value", "" + value);
            if (rowIndex < 0) return '';
            var record = folkStore.getAt(rowIndex);
            return record ? record.get('text') : '';
        };
        function getStockType3(value) {
            if(value==1380)
            	return '厅局级正职（巡视员）';
            else if(value==1381)
            	return '厅局级副职（副巡视员）';
            var rowIndex = rankStore.find("value", "" + value);
            if (rowIndex < 0) return '';
            var record = rankStore.getAt(rowIndex);
            return record ? record.get('text') : '';
        };
        function StringToDate(DateStr) {
        	if (typeof DateStr == "undefined")
        		return new Date();
        	if (typeof DateStr == "date")
        		return DateStr;
        	if(DateStr=='')
        		return new Date();
        	var converted = Date.parse(DateStr);
        	var myDate = new Date(converted);
        	if (isNaN(myDate)) {
        		DateStr = DateStr.replace(/:/g, "-");
        		DateStr = DateStr.replace(" ", "-");
        		DateStr = DateStr.replace(".", "-");
        		var arys = DateStr.split('-');
        		switch (arys.length) {
        			case 7 :
        				myDate = new Date(arys[0], --arys[1], arys[2], arys[3],
        						arys[4], arys[5], arys[6]);
        				break;
        			case 6 :
        				myDate = new Date(arys[0], --arys[1], arys[2], arys[3],
        						arys[4], arys[5]);
        				break;
        			default :
//        				myDate = new Date(arys[0], --arys[1], arys[2]);
        					myDate = new Date(arys[0], --arys[1]);
        				break;
        		};
        	};
        	return myDate;
        };
        var registerDateStr = {
                header: '出生年月',
                dataIndex: 'birthdayStr',
                sortable:false,
//    			readOnly:true,
    			editable:false,
                renderer: function(v, meta, record){
    				meta.style = "white-space:normal;cursor: pointer;";
    				var d  = StringToDate(v);
				    var mmm = d.getMonth() + 1;
				    if(mmm.toString().length==1){
				    	mmm='0'+mmm;
				    }
				    var yyyy = d.getFullYear().toString(); //2011  
				    d=yyyy+'-'+mmm;
    				return "<span style='color: blue;'>" + d + "</span>";
    			}
            };
        this.columns = [{
            header: '学员帐号',
            dataIndex: 'loginID',
            sortable:false,
            width: 75
        },{
            header: '姓名',
            dataIndex: 'userName',
            sortable:false,
            width: 70,
            emptyText:"请输入姓名",
            renderer: function(v, meta, record){
            	if(record.get("isRepeatName") == 999||record.get("isRepeatName") == 0){
                	return v;
            	}
                return '<span style="color:red;">' + v + '</span>';
            },
            blankText:"此项为必输项！"
//            ,editor: {
//                xtype: 'textfield',
//                maxLength:10,
//                allowBlank: false,
//                name: 'c.user_Name'
//            }
        },{
            header: '性别',
            dataIndex: 'sexCode',
            sortable:false,
            width: 60,
            renderer: getStockType1
        },registerDateStr,
        /*{
            header: '出生年月',
            dataIndex: 'birthdayStr',
            width: 70,
            renderer: function(value, meta, record) {
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        },*/
        {
            header: '民族',
            dataIndex: 'folkCode',
            sortable:false,
            width: 60,
            renderer: getStockType2,
            allowBlank: false
        },{
            header: '手机号',
            dataIndex: 'mobileTelephone',
            sortable:false,
            width: 100,
            emptyText:"请输入手机号"
//            blankText:"此项为必输项！",
        },{
        	header: '单位', 
        	dataIndex: 'depName', 
        	flex:.8,
            sortable:false,
        	renderer: function(value, meta, record){
				meta.tdAttr = 'data-qtip="' + value + '"'; 
				return value; 
			} 
        },{
            header: '职级',
            dataIndex: 'rankCode',
            sortable:false,
            flex: .6,
            renderer: getStockType3
        },{
            header: '单位及职务',
            dataIndex: 'position',
            sortable:false,
            flex: 1,
            editable: false
        },{
            header: '身份证',
            dataIndex: 'paperNum',
            width: 150
        },{
        	header: '备注', 
        	dataIndex:'remark',
            sortable:false,
        	minWidth: 100, 
        	renderer:function(value, meta, record){
        		meta.tdAttr = 'data-qtip="' + value + '"'; 
        		return value; 
    		},
            editor: {
                xtype: 'textfield',
                allowBlank: false,
                name: 'c.user_Name'
            }
        }];

        // classId: me.up('window').classId,//培训班Id
        this.store = Ext.create('PJ.' + prototype + '.unitregistrationmanagement.CloseUserList');
        var me = this;
        // 实现排序序号的编辑
        this.plugins = [Ext.create('Ext.grid.plugin.CellEditing', {
            clicksToEdit: 1,
            listeners: {
                beforeedit: function(editor, e) {
                    var textfield = e.column.getEditor();
                	if(e.field!='mobileTelephone'&&e.field!='remark'&&e.field!='folkCode'&&e.field!='position'){
	                    if ((e.record.get("rankCode") == 1380||e.record.get("rankCode") == 1381)) {
	                    	alert("您无权修改。");
	                    	return false;
		        		}
                	}
                    if (gj_onlineLearning_constant.loadType == "ajax") {
                        Ext.Ajax.request({
                            url: tokenUrl,
                            success: function(response) {
                                var data = response.responseText;
                                var token = Ext.decode(data, true);
                                textfield.token = token;
                            }
                        });
                    }
                }
            }
        })];
        this.loadType = gj_onlineLearning_constant.loadType;
        this.callParent(arguments);
    },

    closes: function(button) {
        // this.grid.store.load();
    },
    // 撤销报名
    onUnSignUpClick: function(button) {
        var me = this;
        var records = this.getSelectionModel().getSelection();
        Ext.Msg.confirm({
            title: '提示',
            msg: '您确定要为这些学员撤销报名吗？',
            buttons: Ext.Msg.OKCANCEL,
            fn: function(id) {
                if (id == 'ok') {
                    var classUserIds = new Array();
                    var userIds = new Array();
                    Ext.Array.each(records,function(name, index) {
                        classUserIds.push(name.get("classUserId"));
                        userIds.push(name.get("userId"));
                    });
                    me.getStore().remove(me.getSelectionModel().getSelection());
                } else {
                    return;
                }
            }
        });
    },
    listeners: {
        // 点击列表数据时显示按钮事件
    	selectionchange: function(selModel, selected) {
    	    this.down('#' + this.oldId + "UnSignUp").setDisabled(selected.length === 0); //撤销报名 //
    	}, 
        viewready: function() {
            this.addDocked([{
                xtype: 'toolbar',
                dock: 'top',
                // displayInfo: true,
                items: [{
                    text: '撤销报名',
                    iconCls: 'publishCancelIco',
                    itemId: this.oldId + "UnSignUp",
                    disabled: true,
                    scope: this,
                    handler: this.onUnSignUpClick
                },
                "->", "->", this.countSelectPanel]
            }]);
            var me = this;
            // 触发权限过滤事件
            me.fireEvent("permission", this);
            var params = me.store.proxy.extraParams;
            var newParams = {
                searchClassID: this.grid.classId,
                clickSource: this.grid.win.clickSource,
                searchDepId: this.grid.deptId,
                searchUserIds: this.grid.userIds,
                page:-1
            };
            Ext.apply(params, newParams);
            this.store.isLoad = true;
            this.store.load();
        },
    }
});